﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UpanHelper2
{
    class UpanManager
    {
        
        public static List<MyDrive> myDrives = new List<MyDrive>();
        public static Panel panel2/*想要内嵌的目的地*/, panel3/*无内嵌元素的显示*/;
        public static Label label2, label5;
        public static PictureBox pictureBox6;
        public static bool showable = true;

        public static void refresh()//刷新数据，对当前盘符信息进行整理
        {
            hide();//刷新数据必须需要将原有的数据隐藏，但现有的数据并不需要显示。
                    //在hide中对数据进行了清空。

            DriveInfo[] s = DriveInfo.GetDrives();

            int beginY = 35;//从panel2  的0，35位置开始显示
            foreach (DriveInfo drive in s)
            {
                if (drive.DriveType == DriveType.Removable)
                {
                    MyDrive myDrive = new MyDrive();
                    Form2 form2 = new Form2(drive);

                    //确定新form的位置 和 其他信息
                    form2.Left = 0;
                    form2.Top = beginY;
                    form2.TopLevel = false;//不是最顶层窗体

                    beginY += 70;//修改下一个form所在的位置

                    myDrive.driveInfo = drive;
                    myDrive.form2 = form2;
                    myDrives.Add(myDrive);
                }
            }

        }
        private static void hide()
        {
            for(int i = 0; i < myDrives.Count; i++)
            {
                myDrives[i].form2.Close();
            }
            myDrives.Clear();//清除原有数据
                       
        }
        public static void show()
        {
            label2.Visible = label5.Visible = pictureBox6.Visible = true;
            if (myDrives.Count == 0)
            {
                panel3.Visible = true;
            }
            else
            {
                panel3.Visible = false;
                for (int i = 0; i < myDrives.Count; i++)
                {
                    panel2.Controls.Add(myDrives[i].form2);//将窗体内嵌在panel中
                    myDrives[i].form2.Show();
                }
            }          
        }
        public static void showTool(DriveInfo driveInfo)
        {
            //隐藏无用控件
            label2.Visible = label5.Visible = pictureBox6.Visible = false;


            hide();
            Form3 form3 = new Form3(driveInfo);
            form3.Left = 0;
            form3.Top = 0;
            form3.TopLevel = false;//不是最顶层窗体
            panel2.Controls.Add(form3);
            form3.Show();
        }
        public static bool contain(DriveInfo driveInfo)
        {
            for(int i = 0; i < myDrives.Count; i++)
            {
                if (myDrives[i].driveInfo == driveInfo) return true;
            }
            return false;
        }

    }

    public class MyDrive
    {
        public Form2 form2; 
        public DriveInfo driveInfo;

    }
}
